package com.zxkj.commonpatrol.repoisity;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.download.DownloadInterceptor;
import com.allen.library.download.DownloadObserver;
import com.allen.library.factory.ApiFactory;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.network.AppUrlConfig;
import com.czy.basecommon.utils.tools.SPUtils;
import com.zxkj.commonpatrol.model.entity.SelectProjectEntrity;
import com.zxkj.commonpatrol.model.post.PostAlarmPhotoInfo;
import com.zxkj.commonpatrol.model.post.PostSearchProject;
import com.zxkj.commonpatrol.model.response.SearchProjectResponse;
import com.zxkj.commonpatrol.net.ETFAPi;
import com.zxkj.commonpatrol.net.ETFApiHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;

import static com.zxkj.commonpatrol.net.ETFApiHelper.BASE_URL;

public class SelectProjectsRepoisity extends BaseRepository {
    public SelectProjectsRepoisity() {

    }


    //初始化選擇項目列表
    public void  getSelectProject(String CustomerCode,String Filter,int PageIndex,int PageSize,MyBaseDataCallBack mDataCallBack){

        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID,""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN,""));
        map.put("Lang", 1);
        PostSearchProject  postSearchProject  =new PostSearchProject();
        postSearchProject.setCustomerCode(CustomerCode);
        postSearchProject.setFilter(Filter);
        postSearchProject.setPageIndex(PageIndex);
        postSearchProject.setPageSize(PageSize);

        ETFApiHelper.geRadomApi().getSearchProjects(map,new BaseHttpUtil<PostSearchProject>().convertVo2Json(postSearchProject))
                .compose(Transformer.<SearchProjectResponse>switchSchedulers())
                .subscribe(new CommonObserver<SearchProjectResponse>() {
                    @Override
                    protected String setTag() {
                        return "SelectProject";
                    }

                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(404,errorMsg);
                    }

                    @Override
                    protected void onSuccess(SearchProjectResponse s) {

                        //回调数据
                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });


//        List<SelectProjectEntrity> datas=new ArrayList<>();
//        for (int i = 0; i < 3; i++) {
//            datas.add(new SelectProjectEntrity(i+1+"","100"+i,"深圳"+i,"Project"+i,"ce"+i));
//        }
//        datas.add(new SelectProjectEntrity());
//        mDataCallBack.onDataSuccessCallBack(datas);
//        mDataCallBack.onDataFailCallBack(300,"");
    }

    public void  Test(){
       String fileName="text"+".jpeg";
        ApiFactory.getInstance()
                .setOkClient(new OkHttpClient.Builder().addInterceptor(new DownloadInterceptor()).build())
                .createApi(AppUrlConfig.BASE_KEY, BASE_URL, ETFAPi.class)
                .Text()
                .compose(Transformer.<ResponseBody>switchSchedulers())
                .subscribe(new DownloadObserver(fileName, ItrackConstant.Picture
                ) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(long bytesRead, long contentLength, float progress, boolean done, String filePath) {
                        if (done) {

                        }
                    }
                });

    }


}
